
package com.huawei.workspace.demo;

import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.core.http.HttpConfig;
import com.huaweicloud.sdk.workspace.v2.WorkspaceClient;
import com.huaweicloud.sdk.workspace.v2.model.CreateDesktopUserRequest;
import com.huaweicloud.sdk.workspace.v2.model.CreateDesktopUserResponse;
import com.huaweicloud.sdk.workspace.v2.model.CreateUserReq;
import com.huaweicloud.sdk.workspace.v2.model.DeleteUserRequest;
import com.huaweicloud.sdk.workspace.v2.model.DeleteUserResponse;
import com.huaweicloud.sdk.workspace.v2.model.EditUserReq;
import com.huaweicloud.sdk.workspace.v2.model.ListUsersRequest;
import com.huaweicloud.sdk.workspace.v2.model.ListUsersResponse;
import com.huaweicloud.sdk.workspace.v2.model.UpdateUserInfoRequest;
import com.huaweicloud.sdk.workspace.v2.model.UpdateUserInfoResponse;
import com.huaweicloud.sdk.workspace.v2.region.WorkspaceRegion;

public class WorkspaceUsersManageDemo {

    public static void main(String[] args) {
        // 设置AK和SK
        String ak = "<YOUR AK>";
        String sk = "<YOUR SK>";
        String projectId = "<YOUR PROJECTID>";

        ICredential auth = new BasicCredentials().withAk(ak).withSk(sk).withProjectId(projectId);

        HttpConfig config = HttpConfig.getDefaultHttpConfig();
        config.withIgnoreSSLVerification(true);
        WorkspaceClient client = WorkspaceClient.newBuilder()
            .withCredential(auth)
            .withRegion(WorkspaceRegion.valueOf("cn-north-4")) // 示例中区域为北京四，根据实际调用区域替换
            .withHttpConfig(config)
            .build();
        // 查询用户列表
        listUsers(client);
        // 创建用户
        // createDesktopUser(client);
        // 修改用户信息
        // updateUserInfo(client);
        // 删除指定用户
        // deleteUser(client);
    }

    private static void listUsers(WorkspaceClient workspaceClient) {
        ListUsersRequest request = new ListUsersRequest();
        try {
            ListUsersResponse listUsersResponse = workspaceClient.listUsers(request);
            System.out.println(listUsersResponse.toString());
        } catch (ConnectionException | RequestTimeoutException e) {
            System.out.println(e.getMessage());
        } catch (ServiceResponseException e) {
            System.out.println(e.getHttpStatusCode());
            System.out.println(e.getErrorCode());
            System.out.println(e.getErrorMsg());
        }
    }

    private static void createDesktopUser(WorkspaceClient workspaceClient) {
        String userName = "<your userName string>";
        String userEmail = "<your userEmail string>";
        try {
            CreateUserReq createUserReq = new CreateUserReq();
            createUserReq.setUserName(userName);
            createUserReq.setUserEmail(userEmail);
            CreateDesktopUserResponse createDesktopUserResponse =
                workspaceClient.createDesktopUser(new CreateDesktopUserRequest().withBody(createUserReq));
            System.out.println(createDesktopUserResponse.toString());
        } catch (ConnectionException | RequestTimeoutException e) {
            System.out.println(e.getMessage());
        } catch (ServiceResponseException e) {
            System.out.println(e.getHttpStatusCode());
            System.out.println(e.getErrorCode());
            System.out.println(e.getErrorMsg());
        }
    }

    private static void updateUserInfo(WorkspaceClient workspaceClient) {
        String userId = "{your userId string}";
        try {
            EditUserReq editUserReq = new EditUserReq();
            editUserReq.withDescription("description");
            UpdateUserInfoResponse updateUserInfoResponse =
                workspaceClient.updateUserInfo(new UpdateUserInfoRequest().withBody(editUserReq).withUserId(userId));
            System.out.println(updateUserInfoResponse.toString());
        } catch (ConnectionException | RequestTimeoutException e) {
            System.out.println(e.getMessage());
        } catch (ServiceResponseException e) {
            System.out.println(e.getHttpStatusCode());
            System.out.println(e.getErrorCode());
            System.out.println(e.getErrorMsg());
        }
    }

    private static void deleteUser(WorkspaceClient workspaceClient) {
        String userId = "{your userId string}";
        try {
            DeleteUserResponse deleteUserResponse =
                workspaceClient.deleteUser(new DeleteUserRequest().withUserId(userId));
            System.out.println(deleteUserResponse.toString());
        } catch (ConnectionException | RequestTimeoutException e) {
            System.out.println(e.getMessage());
        } catch (ServiceResponseException e) {
            System.out.println(e.getHttpStatusCode());
            System.out.println(e.getErrorCode());
            System.out.println(e.getErrorMsg());
        }
    }
}
